package read;
	
	import java.util.HashMap;
	import com.jme3.animation.AnimChannel;
	import com.jme3.animation.AnimControl;
	import com.jme3.animation.AnimEventListener;
	import com.jme3.animation.Animation;
	import com.jme3.animation.LoopMode;
	import com.jme3.animation.SpatialTrack;
	import com.jme3.cinematic.MotionPath;
	import com.jme3.cinematic.MotionPathListener;
	import com.jme3.cinematic.events.MotionTrack;
	import com.jme3.math.ColorRGBA;
	import com.jme3.math.FastMath;
	import com.jme3.math.Quaternion;
	import com.jme3.math.Vector3f;
	import com.jme3.scene.Geometry;
	import com.jme3.scene.Node;
	import com.jme3.scene.shape.Box;

	import de.fhkoeln.imp.ca.jme3.framework.CAFramework;
	import de.fhkoeln.imp.ca.jme3.util.Factory;


	public class AnimationTest extends CAFramework implements AnimEventListener, MotionPathListener {
		
		
		private AnimControl rubikAnimControl;
		private MotionTrack motionTrack;
		private AnimChannel explosionChannel;
		private Node rubikNode, vehicleNode;
		
		

		public static void main(String[] args) {
			
			new AnimationTest().start();
		}
		
		public void simpleInitApp() {
			
			initFramework();
			initObjects();
			initAnimations();
		}
		
		private void initObjects() {
			//init Rubik
			Geometry g = new Geometry("Rubik", new Box(Vector3f.ZERO, 1, 1, 1));
			g.setMaterial(Factory.createSimpleLightingMaterial(ColorRGBA.Orange));
			rubikNode = new Node("Rubik Node");
			rubikNode.attachChild(g);
			scene.attachChild(rubikNode);
			
			//init Vehicle
			g = new Geometry("Vehicle", new Box(Vector3f.ZERO, 1, 1, 1));
			g.setMaterial(Factory.createSimpleLightingMaterial(ColorRGBA.Blue));
			vehicleNode = new Node("Vehicle Node");
			vehicleNode.attachChild(g);
			scene.attachChild(vehicleNode);
		}
		
		private void initAnimations() {
			//init rubik animation
			float animationTime = 2;
			float fps = 25;
			int totalFrames = (int)(animationTime * fps);
			
	        Vector3f[] translations = new Vector3f[totalFrames];
	        Quaternion[] rotations = new Quaternion[totalFrames];
	        Vector3f[] scales = new Vector3f[totalFrames];
	        float[] times = new float[totalFrames];
	        
	        for (int i = 0; i < totalFrames; i++) {
				translations[i] = Vector3f.UNIT_XYZ;
				rotations[i] = Quaternion.IDENTITY;
				scales[i] = new Vector3f(1, FastMath.PI % (i + 1), 1);
				times[i] = i;
			}
			
			SpatialTrack st = new SpatialTrack(times, translations, rotations, scales);
			
			Animation sa = new Animation("Explosion", animationTime);
			sa.setTracks(new SpatialTrack[] {st});
			
			rubikAnimControl = new AnimControl();
			HashMap<String, Animation> animations = new HashMap<String, Animation>();
			animations.put("Explosion", sa);
			rubikAnimControl.setAnimations(animations);
			rubikNode.addControl(rubikAnimControl);
			
			explosionChannel = rubikAnimControl.createChannel();
			
			
			//init vehicle animation
			MotionPath path = new MotionPath();
			path.addWayPoint(new Vector3f(10, 0, -10));
			path.addWayPoint(new Vector3f(0, 5, -10));
			path.addWayPoint(new Vector3f(-10, 0, -10));
			path.addWayPoint(new Vector3f(-10, -5, 0));
			path.addWayPoint(new Vector3f(-10, 0, 10));
			path.addWayPoint(new Vector3f(0, 5, 10));
			path.addWayPoint(new Vector3f(10, 0, 10));
			path.addWayPoint(new Vector3f(10, -5, 0));
			path.setCycle(false);
			path.enableDebugShape(assetManager, scene);
			
			motionTrack = new MotionTrack(vehicleNode, path);
			motionTrack.setDirectionType(MotionTrack.Direction.PathAndRotation);
			motionTrack.setRotation(new Quaternion().fromAngleNormalAxis(-FastMath.HALF_PI, Vector3f.UNIT_Y));
			motionTrack.setInitialDuration(2f);
			motionTrack.setSpeed(1);
			motionTrack.setLoopMode(LoopMode.DontLoop);
			motionTrack.play();
			path.addListener(this);
		}

		/* (non-Javadoc)
		 * @see com.jme3.animation.AnimEventListener#onAnimCycleDone(com.jme3.animation.AnimControl, com.jme3.animation.AnimChannel, java.lang.String)
		 */
		@Override
		public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
		
		}

		/* (non-Javadoc)
		 * @see com.jme3.animation.AnimEventListener#onAnimChange(com.jme3.animation.AnimControl, com.jme3.animation.AnimChannel, java.lang.String)
		 */
		@Override
		public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
		}

		/* (non-Javadoc)
		 * @see com.jme3.cinematic.MotionPathListener#onWayPointReach(com.jme3.cinematic.events.MotionTrack, int)
		 */
		@Override
		public void onWayPointReach(MotionTrack motionControl, int wayPointIndex) {
			if (wayPointIndex == 7) {
				System.out.println("boom");
				//rubikAnimControl.setEnabled(true);
				explosionChannel.setAnim("Explosion");
				explosionChannel.setLoopMode(LoopMode.DontLoop);
			}
		}
	}

	
}
